package com.mujin.keji.collectionsystem.widget;

import android.app.Activity;
import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.mujin.keji.collectionsystem.R;
import com.mujin.keji.collectionsystem.base.BaseDto;
import com.mujin.keji.collectionsystem.listeners.CustomItemClickListener;
import com.mujin.keji.collectionsystem.module.outboundtask.adapter.SelectAssistCommissionerAdapter;
import com.mujin.keji.collectionsystem.module.outboundtask.dto.QueryUserDto;
import com.mujin.keji.collectionsystem.module.outboundtask.dto.UserBean;
import com.mujin.keji.collectionsystem.module.outboundtask.viewmodel.BoundTaskListViewModel;
import com.mujin.keji.collectionsystem.module.outboundtask.views.BoundDetailActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * 选择协助专员popwindow
 *
 * @author weishixiong
 * @Time 2018-05-10
 */


public class SelectAssistCommissionerWindow extends PopupWindow implements CustomItemClickListener {
    private Activity context;
    private RecyclerView recyclerview;
    private LinearLayoutManager linearLayoutManager;
    private EditText etName;
    private TextView tvCancel;
    private SelectAssistCommissioner lisenter;
    private BoundTaskListViewModel boundTaskListViewModel;
    private List<UserBean> datas;
    private SelectAssistCommissionerAdapter selectAssistCommissionerAdapter;

    @Override
    public void onItemClick(View view, int position) {
        lisenter.onSelectAssistCommissioner(datas.get(position));
    }

    public interface SelectAssistCommissioner {

        void onSelectAssistCommissioner(UserBean assistcommissioner);
    }

    /**
     * @param context
     */
    public SelectAssistCommissionerWindow(final BoundDetailActivity context, final SelectAssistCommissioner lisenter) {
        datas = new ArrayList<>();
        boundTaskListViewModel = ViewModelProviders.of(context).get(BoundTaskListViewModel.class);
        linearLayoutManager = new LinearLayoutManager(context);
        this.context = context;
        this.lisenter = lisenter;
        //获得 LayoutInflater 的实例
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View contentView = inflater.inflate(R.layout.pop_selectassistcommissioner, null);
        this.setContentView(contentView);
        recyclerview = (RecyclerView) contentView.findViewById(R.id.recyclerview);
        etName = (EditText) contentView.findViewById(R.id.et_name);
        etName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (TextUtils.isEmpty(s.toString())) {
                    datas.clear();
                    selectAssistCommissionerAdapter.notifyDataSetChanged();
                    recyclerview.setVisibility(View.GONE);

                } else {
                    boundTaskListViewModel.getUserDtoLiveData(s.toString()).observe(context, new Observer<BaseDto<QueryUserDto>>() {
                        @Override
                        public void onChanged(@Nullable BaseDto<QueryUserDto> queryUserDtoBaseDto) {
                            try {
                                datas.clear();
                                datas.addAll(queryUserDtoBaseDto.getData().getLists());
                                selectAssistCommissionerAdapter.notifyDataSetChanged();
                                recyclerview.setVisibility(View.VISIBLE);
                            } catch (Exception e) {

                            }

                        }
                    });


                }


            }
        });
        tvCancel = (TextView) contentView.findViewById(R.id.tv_cancel);
        recyclerview.setLayoutManager(linearLayoutManager);
        setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        this.setFocusable(true);
        this.setOutsideTouchable(true);
//        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(Color.parseColor("#80000000"));
        // 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ，设置其他控件变化等操作
        setBackgroundDrawable(dw);
        selectAssistCommissionerAdapter = new SelectAssistCommissionerAdapter(context, datas, this, this);
        recyclerview.setAdapter(selectAssistCommissionerAdapter);
        recyclerview.setVisibility(View.VISIBLE);
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

    }

}
